<template>
  <el-container>
    <el-header class="header">
      <headerSearch ref="search" @searchFn="getClassList(1)"></headerSearch>
    </el-header>
    <el-main>
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="1">
              <el-button
                type="primary"
                icon="el-icon-circle-plus-outline"
                class="btn"
                @click="openAddClass"
                >新建</el-button
              >
            </el-col>
          </el-row>
        </el-header>
        <el-main class="bmain">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column type="index" label="序号" width="100">
            </el-table-column>
            <el-table-column prop="className" label="商品类型名称" width="1400">
            </el-table-column>
            <el-table-column prop="address" label="操作">
              <template #default="{ row }">
                <span class="edit" @click="openAddClass(row, 1)">修改</span>
                <span class="del" @click="dele(row)">删除</span>
              </template>
            </el-table-column>
          </el-table>
          <el-row class="page" type="flex">
            <el-col :span="2">共{{ detial.totalCount }}条记录</el-col>
            <el-col :span="2"
              >第 {{ params.pageIndex + '/' + detial.totalPage }} 页</el-col
            >
            <el-col :span="16"></el-col>
            <el-col :span="2"
              ><button :disabled="params.pageIndex === 1" @click="change(-1)">
                上一页
              </button></el-col
            >
            <el-col :span="2"
              ><button
                :disabled="params.pageIndex >= +detial.totalPage"
                @click="change(1)"
              >
                下一页
              </button></el-col
            >
          </el-row>
        </el-main>
        <addClass
          @flash="getClassList"
          :dialogInfo="dialogInfo"
          ref="addClass"
        ></addClass>
      </el-container>
    </el-main>
  </el-container>
</template>

<script>
import headerSearch from './components/headerSearch.vue'
import addClass from './components/addClass.vue'
import { getClassList, delGoods } from '@/api/gdInfo'
export default {
  name: 'Dkd3SkuClass',
  components: {
    headerSearch,
    addClass
  },
  data () {
    return {
      tableData: [],
      params: {
        pageIndex: 1,
        pageSize: 10
      },
      detial: {},
      dialogInfo: {}
    }
  },

  mounted () {
    this.getClassList()
  },

  methods: {
    async openAddClass (val, num) {
      if (num) {
        this.dialogInfo = val
      } else {
        this.dialogInfo = {}
      }
      // console.log(num)
      // console.log(this.dialogInfo)
      // this.$refs.addClass.setForm()
      this.$refs.addClass.dialogVisible = true
    },
    async dele (val) {
      console.log(val)
      try {
        await delGoods(val.classId)
        this.$message.success('删除成功')
        this.getClassList()
      } catch (err) {
        this.$message.error('删除失败')
      }
    },
    async getClassList (val) {
      const content = this.$refs.search.formInline.type
      if (val) {
        this.params.className = content
        this.params.pageIndex = 1
      }
      const res = await getClassList(this.params)
      this.tableData = res.currentPageRecords
      this.detial.totalPage = res.totalPage
      this.detial.totalCount = res.totalCount
      // console.log(res)
    },
    change (val) {
      this.params.pageIndex += val
      this.getClassList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-main {
  background-color: #fff;
  .edit {
    cursor: pointer;
    margin-right: 10px;
    color: #a2ceff;
  }
  .del {
    cursor: pointer;
    color: #ff5a5a;
  }
}
.el-header {
  padding: 0;
}
.header {
  margin-bottom: 10px;
}
.page {
  button {
    cursor: pointer;
    width: 70px;
    height: 32px;
    text-align: center;
    line-height: 32px;
    background-color: #d5ddf8;
    border: none;
  }
}
.el-table {
  margin-bottom: 10px;
}
.bmain {
  padding-bottom: 0;
}
</style>
